package xyz.riceball.job.controller;

import org.springframework.web.bind.annotation.*;
import xyz.riceball.framework.core.annotation.RbLog;
import xyz.riceball.framework.core.bean.ApiController;
import xyz.riceball.framework.core.bean.ApiR;
import xyz.riceball.framework.core.bean.PageParam;
import xyz.riceball.job.entity.dto.JobTriggerDTO;
import xyz.riceball.job.entity.po.JobTriggerPO;
import xyz.riceball.job.service.JobService;

import javax.annotation.Resource;

/**
 * <p>
 * 定时任务调度 控制层
 * </p>
 *
 * @author xiaovcloud
 * @since 2022/6/12 21:20
 */
@RestController
@RequestMapping("/rb/api/job")
public class JobController extends ApiController {

    @Resource
    private JobService jobService;

    /**
     * 新增任务
     * @param jobTriggerDto
     * @return
     */
    @PostMapping
    @RbLog(description = "新增任务")
    public ApiR addJob(@RequestBody JobTriggerDTO jobTriggerDto) {
        JobTriggerPO jobTriggerPO = jobService.addJob(jobTriggerDto);
        return ApiR.okMsgAndData("新增成功", jobTriggerPO);
    }

    /**
     * 查询单个任务
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @RbLog(description = "查询单个任务")
    public ApiR deleteJob(@PathVariable String id) {
        jobService.deleteJob(id);
        return ApiR.ok();
    }

    /**
     * 停止任务
     * @param id
     * @return
     */
    @PostMapping("pause/{id}")
    @RbLog(description = "停止任务")
    public ApiR pauseJob(@PathVariable String id) {
        jobService.pauseJob(id);
        return ApiR.ok();
    }

    /**
     * 重启任务
     * @param id
     * @return
     */
    @PostMapping("resume/{id}")
    @RbLog(description = "重启任务")
    public ApiR resumeJob(@PathVariable String id) {
        jobService.resumeJob(id);
        return ApiR.ok();
    }

    /**
     * 修改任务
     * @param jobTriggerDto
     * @return
     */
    @RbLog(description = "修改任务")
    @PutMapping
    public ApiR cornJob(@RequestBody JobTriggerDTO jobTriggerDto) {
        jobService.cornJob(jobTriggerDto);
        return ApiR.ok();
    }

    /**
     * 分页查询任务
     * @param pageParam
     * @param jobTriggerPO
     * @return
     */
    @RbLog(description = "分页查询任务")
    @GetMapping
    public ApiR selectPage(PageParam pageParam, JobTriggerPO jobTriggerPO) {
        return success(jobService.selectPage(PageParam.of(pageParam), jobTriggerPO));
    }

}
